//
//  AJTTAddStoryController.swift
//  JourneyThroughTime
//
//  Created by iOS-Czz on 2023/11/6.
//

import UIKit

import ProgressHUD
class AJTTAddStoryController: AJTTBaseViewController {
    @IBOutlet var bgView: UIView!
    
    @IBOutlet var converPic: UIImageView!
    @IBOutlet var contentTV: UITextView!
    @IBOutlet var nameTf: UITextField!
    var storyPic:UIImage? = nil
    override func viewDidLoad() {
        super.viewDidLoad()
        self.contentTV.addPlaceholder("Please enter", color: .lightGray)
        // Do any additional setup after loading the view.
    }
   
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.wx_navigationBar.isHidden = true
    }
    @IBAction func clickBackAction(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        self.bgView.addRoundCorners(topLeftRadius: 15, topRightRadius: 15, bottomLeftRadius: 0, bottomRightRadius: 0, borderWidth: 0, borderColor: .red)
    }
    @IBAction func clickSaveAction() {
        self.view.endEditing(true)
        if self.nameTf.text!.isEmpty {
            ProgressHUD.showError("Please enter story name")
            return
        }
        if self.storyPic == nil{
            ProgressHUD.showError("Please select story picture")
            return
        }
        if self.contentTV.text!.isEmpty {
            ProgressHUD.showError("Please enter story content")
            return
        }
        let model = AJTTStoryModel()
        model.name = self.nameTf.text!
        model.content = self.contentTV.text!
        model.picture = UIImage.saveImageInCache(self.storyPic!)!
        model.date = Date().toString(format: "yyyy-MM-dd")
        model.isDefault = false
        model.saveToDB()
        ProgressHUD.showSuccess("Story addition succeed")
        let helper = AJTTStoryModel.getUsingLKDBHelper()
        helper.delete(with: AJTTStoryModel.self, where: "isDefault = true")
        let delay = 1.5 // 2秒
        let dispatchTime = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
            self.navigationController?.popToRootViewController(animated: true)
        }
    }
    @IBAction func tapChooseStoryPictureAction(_ sender: Any) {
        showPickImageVC()
    }
}
extension AJTTAddStoryController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func showPickImageVC() {
        let photoPickerViewController:UIImagePickerController = UIImagePickerController()
        photoPickerViewController.sourceType = UIImagePickerController.SourceType.photoLibrary
        photoPickerViewController.delegate = self
        photoPickerViewController.modalPresentationStyle = .fullScreen
        self.present(photoPickerViewController, animated: true, completion: nil)
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        guard let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {
            fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
        }
        self.converPic.image = selectedImage
        self.storyPic = selectedImage
        picker.dismiss(animated: true, completion: nil)
    }
}
